app.controller('EditUserInfoCtrl', ['$scope', '$http', '$stateParams', 'ionicMaterialInk', '$localstorage', '$ionicModal', 'API', '$ionicLoading', '$ionicPopup', function ($scope, $http, $stateParams, ionicMaterialInk, $localstorage, $ionicModal, API, $ionicLoading, $ionicPopup) {
    ionicMaterialInk.displayEffect();

    $scope.shouldShowDelete = false;
    $scope.shouldShowReorder = false;
    $scope.listCanSwipe = true

    //加载
    var loadingShow = function () {
        $ionicLoading.show({
            template: '<div class="loader"><svg class="circular"><circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10"/></svg></div>'
        });
    };
    var loadingHide = function () {
        $ionicLoading.hide();
    };

    //微信客户信息
    $scope.weiInfo = $localstorage.getObject('weiInfo');
    //表单信息
    $scope.userObj = {};
    //列表信息
    $scope.userInfo = {};

    //获取用户信息
    var getData = function () {
        $http.get(API.baseURL + '/editUser/getCustomerInfo').success(function (data) {
            $scope.userInfo = data.data;
            $scope.userObj = {
                customer_id: $scope.userInfo.id,
                telephone: $scope.userInfo.telephone,
                name: $scope.userInfo.name,
                email: $scope.userInfo.email
            };
            console.log($scope.userInfo)
        });
    };
    //获取地址
    var getAddress = function () {
        $http.post(API.baseURL + '/editUser/getAddress', {customer_id: $scope.userObj.customer_id}).success(function (data) {
            $scope.userInfo.addresses = data.data;
            $scope.activeAddresses = data.active.address;
        });
    };

    getData();

    /*手机号*/
    //打开手机号
    $ionicModal.fromTemplateUrl('phoneModal.html', {
        scope: $scope,
        animation: 'slide-in-up'
    }).then(function (modal) {
        $scope.phoneModal = modal;
    });
    $scope.openPhoneModal = function () {
        $scope.phoneModal.show();
    };
    $scope.closePhoneModal = function () {
        $scope.phoneModal.hide();
    };
    //保存手机号
    $scope.saveTelephone = function () {
        if ($scope.checkPhone($scope.userObj.telephone) == 'no') {
            var alertPopup = $ionicPopup.alert({
                title: '提示!',
                template: '手机号码格式不对哦'
            });
            alertPopup.then(function (res) {
                //$scope.formData.clientPhone = '';
            });
        } else {
            loadingShow();
            $http.post(API.baseURL + '/editUser/saveTelephone', {telephone: $scope.userObj.telephone}).success(function () {
                loadingHide();
                $scope.closePhoneModal();
                $scope.userInfo.telephone = $scope.userObj.telephone;
            });
        }
    };

    /*姓名*/
    //打开姓名
    $ionicModal.fromTemplateUrl('nameModal.html', {
        scope: $scope,
        animation: 'slide-in-up'
    }).then(function (modal) {
        $scope.nameModal = modal;
    });
    $scope.openNameModal = function () {
        $scope.nameModal.show();
    };
    $scope.closeNameModal = function () {
        $scope.nameModal.hide();
    };
    //保存姓名
    $scope.saveName = function () {
        loadingShow();
        $http.post(API.baseURL + '/editUser/saveName', {name: $scope.userObj.name}).success(function () {
            loadingHide();
            $scope.closeNameModal();
            $scope.userInfo.name = $scope.userObj.name;
        });
    };

    /*邮箱*/
    //打开邮箱
    $ionicModal.fromTemplateUrl('emailModal.html', {
        scope: $scope,
        animation: 'slide-in-up'
    }).then(function (modal) {
        $scope.emailModal = modal;
    });
    $scope.openEmailModal = function () {
        $scope.emailModal.show();
    };
    $scope.closeEmailModal = function () {
        $scope.emailModal.hide();
    };
    //保存邮箱
    $scope.saveEmail = function () {
        if ($scope.checkEmail($scope.userObj.email) == 'no') {
            var alertPopup = $ionicPopup.alert({
                title: '提示!',
                template: '邮箱格式不对哦'
            });
            alertPopup.then(function (res) {
                //$scope.formData.clientPhone = '';
            });
        } else {
            loadingShow();
            $http.post(API.baseURL + '/editUser/saveEmail', {email: $scope.userObj.email}).success(function () {
                loadingHide();
                $scope.closeEmailModal();
                $scope.userInfo.email = $scope.userObj.email;
            });
        }
    };

    /*地址*/
    //打开管理地址
    $ionicModal.fromTemplateUrl('addressModal.html', {
        scope: $scope,
        animation: 'slide-in-up'
    }).then(function (modal) {
        $scope.addressModal = modal;
    });
    $scope.openAddressModal = function () {
        getAddress();//获取地址

        $http.get("json/city.json").success(function (cityjson) {
            $scope.g = cityjson;
        });
        $scope.addressModal.show();

    };
    $scope.closeAddressModal = function () {
        $scope.addressModal.hide();
    };
    //管理
    $scope.toggleManageNum = 0;
    $scope.toggleManage = function () {
        $scope.toggleManageNum = $scope.toggleManageNum == 0 ? 1 : 0;
    };
    //打开添加地址
    $ionicModal.fromTemplateUrl('addAddress.html', {
        scope: $scope,
        animation: 'slide-in-up'
    }).then(function (modal) {
        $scope.addAddressModal = modal;
    });
    $scope.openAddAddressModal = function () {
        $http.get("json/city.json").success(function (cityjson) {
            $scope.g = cityjson;
        })
        $scope.addAddressModal.show();
    };
    $scope.closeAddAddressModal = function () {
        $scope.addAddressModal.hide();
    };
    //保存新地址
    $scope.ihg = {};
    $scope.nat = {};
    $scope.city = {};
    $scope.saveNewAddress = function () {
        if ($scope.checkPhone($scope.userObj.tel_telephone) == 'no' || !$scope.userObj.tel_telephone) {
            var alertPopup = $ionicPopup.alert({
                title: '提示!',
                template: '手机号码格式不对哦'
            });
            alertPopup.then(function (res) {
            });
        } else if (!$scope.userObj.address) {
            var alertPopup = $ionicPopup.alert({
                title: '提示!',
                template: '地址不能为空'
            });
            alertPopup.then(function (res) {
            });
        } else if (!$scope.userObj.moreAddress) {
            var alertPopup = $ionicPopup.alert({
                title: '提示!',
                template: '详细地址不能为空'
            });
            alertPopup.then(function (res) {
            });
        } else if (!$scope.userObj.tel_name) {
            var alertPopup = $ionicPopup.alert({
                title: '提示!',
                template: '联系人姓名不能为空'
            });
            alertPopup.then(function (res) {
            });

        } else {
            loadingShow();
            $http.post(API.baseURL + '/editUser/addAddress', {
                customer_id: $scope.userObj.customer_id,
                address: $scope.userObj.address,
                tel_telephone: $scope.userObj.tel_telephone,
                tel_name: $scope.userObj.tel_name
            }).success(function () {
                loadingHide();
                $scope.closeAddAddressModal();
                //getData();
                getAddress();
            });
        }
    };
    //打开修改地址
    $ionicModal.fromTemplateUrl('editAddress.html', {
        scope: $scope,
        animation: 'slide-in-up'
    }).then(function (modal) {
        $scope.editAddressModal = modal;
    });
    $scope.openEditAddressModal = function () {
        $http.get("json/city.json").success(function (cityjson) {
            $scope.g = cityjson;
        })
        $scope.editAddressModal.show();
    };
    $scope.closeEditAddressModal = function () {
        $scope.editAddressModal.hide();
    };
    $scope.editAddress = function (item) {
        $scope.openEditAddressModal();
        $scope.ihg = {};
        $scope.nat = {};
        $scope.city = {};
        $scope.editUserObj = item;

    };
    //保存修改地址
    $scope.saveEditAddress = function () {
        if ($scope.checkPhone($scope.editUserObj.tel_telephone) == 'no' || !$scope.editUserObj.tel_telephone) {
            var alertPopup = $ionicPopup.alert({
                title: '提示!',
                template: '手机号码格式不对哦'
            });
            alertPopup.then(function (res) {
            });
        } else if (!$scope.editUserObj.address) {
            var alertPopup = $ionicPopup.alert({
                title: '提示!',
                template: '地址不能为空'
            });
            alertPopup.then(function (res) {
            });
        } else if (!$scope.editUserObj.tel_name) {
            var alertPopup = $ionicPopup.alert({
                title: '提示!',
                template: '联系人姓名不能为空'
            });
            alertPopup.then(function (res) {
            });
        } else {
            loadingShow();
            $http.post(API.baseURL + '/editUser/updateAddress', {
                address_id: $scope.editUserObj.id,
                address: $scope.editUserObj.address,
                tel_telephone: $scope.editUserObj.tel_telephone,
                tel_name: $scope.editUserObj.tel_name
            }).success(function () {
                loadingHide();
                $scope.closeEditAddressModal();
                getAddress();//获取地址
            });
        }
    };
    //删除地址
    $scope.delAddress = function (item) {
        var confirmPopup = $ionicPopup.confirm({
            title: '警告!',
            template: '确定要删除吗?',
            okText: '确认',
            okType: 'button-energized-900',
            cancelText: '取消'
        });

        confirmPopup.then(function (res) {
            if (res) {
                loadingShow();
                $http.post(API.baseURL + '/editUser/delAddress', {
                    address_id: item.id,
                }).success(function () {
                    getAddress();//获取地址
                    loadingHide();
                });
            } else {
                console.log('You are not sure');
            }
        });
    };
//选择默认地址
    $scope.chooseDefault = function (item) {
        loadingShow();
        $http.post(API.baseURL + '/editUser/setDefaultAddress', {
            address_id: item.id,
            customer_id: item.customer_id
        }).success(function () {
            getAddress();//获取地址
            loadingHide();
        });
        console.log(item);
    }

    //校验手机号
    $scope.checkPhone = function (val) {
        if (val) {
            if (!val.match(/^1[3|4|5|7|8][0-9]\d{8}$/)) {
                return 'no';
            } else {
                return 'yes';
            }
        }
    }
    //校验邮箱
    $scope.checkEmail = function (val) {
        if (val) {
            var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            if (!filter.test(val)) {
                return 'no';
            }
            else {
                return 'yes';
            }
        }
    }

}]);